home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / D-G / GemsI / Original / Quaternions.c < prev    next >
Encoding:
Text File  |  1992-06-16  |  2.1 KB  |  114 lines  |  [TEXT/MPS ]

  1. /*
  2. Using Quaternions for Coding 3D Transformations
  3. Patrick-Gilles Maillot
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. set_obs_position(x,y,z)
  8. float    x, y, z;
  9. {
  10. int    i;
  11.  
  12. /*
  13.  * Set the values of the eye's position.
  14.  * The position here represents the position of the orthonormal base
  15.  * in respect to the observer.
  16.  */
  17.        P[0] = -x;
  18.        P[1] = -y;
  19.        P[2] = -z;
  20. /*
  21.  * Set the visualization to be in the decreasing x axis
  22.  */
  23.     Q[0] = 1.;
  24.       for (i = 1; i < 4; i++) Q[i] = 0.;
  25. }
  26.  
  27. translate_quaternion(x,i,w)
  28. float    x;
  29. int    i, w;
  30. {
  31. int    j, k;
  32. float    A, B, D, E, F;
  33.     
  34.     if (w < 0) {
  35. /*
  36.  * The observer moves in respect to the scene.
  37.  */
  38.     P[i - 1] -= x;
  39.   } else {
  40.  
  41. /* 
  42.  * The scene moves in respect to the observer.
  43.  * Compute the successor axis of i [1,2,3];
  44.  * and then the successor axis of j [1,2,3];
  45.  */
  46.     if ((j = i + 1) > 3) j = 1;
  47.     if ((k = j + 1) > 3) k = 1;
  48.     A = Q[j]; B = Q[k]; F = Q[0]; E = Q[i];
  49.     P[i - 1] += x * (E * E + F * F - A * A - B * B);
  50.     D = x + x;
  51.     P[j - 1] += D * (E * A + F * B);
  52.     P[k - 1] += D * (E * B + F * A);
  53.   }
  54. }
  55.  
  56. rotate_quaternion(x,y,i,w)
  57. float    x, y;
  58. int    i, w;
  59. {
  60. int    j, k;
  61. float    E, F, R1;
  62. /*
  63.  * Compute the successor axis of i [1,2,3] and  j [1,2,3];
  64.  */
  65.     if ((j = i + 1) > 3) j = 1;
  66.     if ((k = j + 1) > 3) k = 1;
  67.     E = Q[i];
  68.     Q[i] = E * x + w * y * Q[0];
  69.     Q[0] = Q[0] * x - w * y * E;
  70.     E = Q[j];
  71.     Q[j] = E * x + y * Q[k];
  72.     Q[k] = Q[k] * x - y * E;
  73.       if (w < 0) {
  74. /* Compute a new position if the observer moves in respect to the scene. */
  75.         j -= 1; k -= 1;
  76.         R1 = x * x - y * y;
  77.         F = 2. * x * y;
  78.         E = P[j];
  79.         P[j] = E * R1 + F * P[k];
  80.         P[k] = P[k] * R1 - F * E;
  81.       }
  82. }
  83.  
  84.  
  85. Evaluate_matrix()
  86. {
  87. float    e, f, r[4];
  88. int    i, j, k;
  89. /*
  90.  * We will need some square values!
  91.  */
  92.     for (i = 0; i < 4; i++) r[i] = Q[i] * Q[i];
  93. /*
  94.  * Compute each element of the matrix.
  95.  * j is the successor of i (in 1,2,3), while k is the successor of j.
  96.  */
  97.       for (i = 1; i < 4; i++) {
  98.         if ((j = i + 1) > 3) j = 1;
  99.         if ((k = j + 1) > 3) k = 1;
  100.         e = 2. * Q[i] * Q[j];
  101.         f = 2. * Q[k] * Q[0];
  102.         M[j][i] = e - f;
  103.         M[i][j] = e + f;
  104.         M[i][i] = r[i] + r[0] - r[j] - r[k];
  105.         M[0][i] = P[i - 1];
  106.         M[i][0] = 0.;
  107.       }
  108.     M[0][0] = 1.;
  109. }
  110.  
  111.  
  112.  
  113.  
  114.